package com.example.springaop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class MyAspectDemo {

    @Around("@annotation( com.example.springaop.config.MyAspect)")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("do around before");
        Object proceed = null;
        try {
          proceed = joinPoint.proceed();
        }catch (Exception e){
            log.info("发生异常:"+e);
        }
        log.info("do around after");
        return proceed;
    }

    /**
     * 对所有requestMapping方法生效
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@annotation( org.springframework.web.bind.annotation.RequestMapping)")
    public Object doMappingAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("do RequestMapping around before");
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        }catch (Exception e){
            log.info("发生异常:"+e);
        }
        log.info("do RequestMapping around after");
        return proceed;
    }
}
